package cn.youqu.happy.module.youqu.controller.admin.userBlack;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.userBlack.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userBlack.UserBlackDO;
import cn.youqu.happy.module.youqu.convert.userBlack.UserBlackConvert;
import cn.youqu.happy.module.youqu.service.userBlack.UserBlackService;

@Api(tags = "管理后台 - 用户黑名单")
@RestController
@RequestMapping("/youqu/user-black")
@Validated
public class UserBlackController {

    @Resource
    private UserBlackService userBlackService;

    @PostMapping("/create")
    @ApiOperation("创建用户黑名单")
    @PreAuthorize("@ss.hasPermission('youqu:user-black:create')")
    public CommonResult<Integer> createUserBlack(@Valid @RequestBody UserBlackCreateReqVO createReqVO) {
        return success(userBlackService.createUserBlack(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户黑名单")
    @PreAuthorize("@ss.hasPermission('youqu:user-black:update')")
    public CommonResult<Boolean> updateUserBlack(@Valid @RequestBody UserBlackUpdateReqVO updateReqVO) {
        userBlackService.updateUserBlack(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户黑名单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-black:delete')")
    public CommonResult<Boolean> deleteUserBlack(@RequestParam("id") Integer id) {
        userBlackService.deleteUserBlack(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户黑名单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-black:query')")
    public CommonResult<UserBlackRespVO> getUserBlack(@RequestParam("id") Integer id) {
        UserBlackDO userBlack = userBlackService.getUserBlack(id);
        return success(UserBlackConvert.INSTANCE.convert(userBlack));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户黑名单列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-black:query')")
    public CommonResult<List<UserBlackRespVO>> getUserBlackList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserBlackDO> list = userBlackService.getUserBlackList(ids);
        return success(UserBlackConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户黑名单分页")
    @PreAuthorize("@ss.hasPermission('youqu:user-black:query')")
    public CommonResult<PageResult<UserBlackRespVO>> getUserBlackPage(@Valid UserBlackPageReqVO pageVO) {
        PageResult<UserBlackDO> pageResult = userBlackService.getUserBlackPage(pageVO);
        return success(UserBlackConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户黑名单 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:user-black:export')")
    @OperateLog(type = EXPORT)
    public void exportUserBlackExcel(@Valid UserBlackExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserBlackDO> list = userBlackService.getUserBlackList(exportReqVO);
        // 导出 Excel
        List<UserBlackExcelVO> datas = UserBlackConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户黑名单.xls", "数据", UserBlackExcelVO.class, datas);
    }

}
